// 设备状态管理
import { defineStore } from 'pinia'

export interface DeviceInfo {
  id: string
  name: string
  location: string
  status: 'available' | 'occupied' | 'maintenance' | 'offline'
  type: 'standard' | 'premium'
  price: number
  waitTime: number // 等待时间（分钟）
}

interface DeviceState {
  devices: DeviceInfo[]
  currentDevice: DeviceInfo | null
}

export const useDeviceStore = defineStore('device', {
  state: (): DeviceState => ({
    devices: [],
    currentDevice: null
  }),
  
  getters: {
    getDevices: (state: DeviceState) => state.devices,
    getCurrentDevice: (state: DeviceState) => state.currentDevice,
    getAvailableDevices: (state: DeviceState) => 
      state.devices.filter(device => device.status === 'available')
  },
  
  actions: {
    // 设置设备列表
    setDevices(devices: DeviceInfo[]) {
      this.devices = devices
    },
    
    // 设置当前设备
    setCurrentDevice(device: DeviceInfo) {
      this.currentDevice = device
    },
    
    // 更新设备状态
    updateDeviceStatus(deviceId: string, status: DeviceInfo['status']) {
      const device = this.devices.find(d => d.id === deviceId)
      if (device) {
        device.status = status
      }
      
      if (this.currentDevice && this.currentDevice.id === deviceId) {
        this.currentDevice.status = status
      }
    },
    
    // 初始化设备数据（模拟）
    initDevices() {
      // 模拟设备数据
      const mockDevices: DeviceInfo[] = [
        {
          id: 'D001',
          name: '1号洗车机',
          location: 'A区01号',
          status: 'available',
          type: 'standard',
          price: 15,
          waitTime: 0
        },
        {
          id: 'D002',
          name: '2号洗车机',
          location: 'A区02号',
          status: 'occupied',
          type: 'standard',
          price: 15,
          waitTime: 5
        },
        {
          id: 'D003',
          name: '3号洗车机',
          location: 'B区01号',
          status: 'available',
          type: 'premium',
          price: 25,
          waitTime: 0
        },
        {
          id: 'D004',
          name: '4号洗车机',
          location: 'B区02号',
          status: 'maintenance',
          type: 'premium',
          price: 25,
          waitTime: 0
        }
      ]
      
      this.setDevices(mockDevices)
    }
  }
})